package com.fenghuajueli.module_user.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.fenghuajueli.libbasecoreui.listener.OnListClickListener;
import com.fenghuajueli.lib_data.entity.goods.GoodsEntity;
import com.fenghuajueli.module_user.R;

import java.util.List;

public class VipPriceAdapter extends RecyclerView.Adapter<VipPriceAdapter.ViewHolder> {
    private Context mContext;
    private List<GoodsEntity> goodsEntityList;
    private int currentPosition = 0;
    private SpannableString spannableString;
    private StrikethroughSpan strikethroughSpan;
    private OnListClickListener<GoodsEntity> onItemClick;

    public VipPriceAdapter(Context mContext, List<GoodsEntity> goodsEntityList) {
        this.mContext = mContext;
        this.goodsEntityList = goodsEntityList;
        strikethroughSpan = new StrikethroughSpan();
    }

    @NonNull
    @Override
    public VipPriceAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.vip_price_item_layout, parent, false);
        return new ViewHolder(view);
    }

    public void setOnItemClick(OnListClickListener<GoodsEntity> onItemClick) {
        this.onItemClick = onItemClick;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        GoodsEntity goodsEntity = goodsEntityList.get(position);
        holder.tvVipName.setText(goodsEntity.getGoods_name());
        String originPrice = "原价" + goodsEntity.getOriginal_price();
        spannableString = new SpannableString(originPrice);
        spannableString.setSpan(strikethroughSpan, 0, originPrice.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        holder.tvOriginPrice.setText(spannableString);
        holder.tvFirstPrice.setText(String.valueOf(goodsEntity.getGoods_price()));
        if (currentPosition == position) {
            holder.ivSelectedTips.setVisibility(View.VISIBLE);
            holder.tvFirstPrice.setTextColor(Color.parseColor("#80481F"));
            holder.tvRmbSymbol.setTextColor(Color.parseColor("#80481F"));
            holder.tvOriginPrice.setTextColor(Color.parseColor("#80481F"));
            holder.tvVipName.setTextColor(Color.parseColor("#80481F"));
            holder.itemContainer.setBackgroundResource(R.mipmap.aa_xz);
        } else {
            holder.ivSelectedTips.setVisibility(View.GONE);
            holder.tvFirstPrice.setTextColor(Color.parseColor("#93949E"));
            holder.tvRmbSymbol.setTextColor(Color.parseColor("#93949E"));
            holder.tvOriginPrice.setTextColor(Color.parseColor("#93949E"));
            holder.tvVipName.setTextColor(Color.parseColor("#93949E"));
            holder.itemContainer.setBackgroundResource(R.mipmap.aa_wxz);
        }
        holder.itemContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setCurrentPosition(position);
                if (onItemClick != null) {
                    onItemClick.itemClick(position, goodsEntity);
                }
            }
        });
    }


    public int getCurrentPosition() {
        return currentPosition;
    }


    public void setCurrentPosition(int currentPosition) {
        this.currentPosition = currentPosition;
        notifyDataSetChanged();
    }

    public void refreshData(List<GoodsEntity> goodsEntityList) {
        this.goodsEntityList = goodsEntityList;
        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        return goodsEntityList.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {

        public RelativeLayout itemContainer;
        public TextView tvVipName;
        public TextView tvFirstPrice;
        public TextView tvOriginPrice;
        public TextView ivSelectedTips;
        public TextView tvRmbSymbol;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            itemContainer = itemView.findViewById(R.id.itemContainer);
            tvVipName = itemView.findViewById(R.id.tvVipName);
            tvFirstPrice = itemView.findViewById(R.id.tvFirstPrice);
            tvOriginPrice = itemView.findViewById(R.id.tvOriginPrice);
            ivSelectedTips = itemView.findViewById(R.id.ivSelectedTips);
            tvRmbSymbol = itemView.findViewById(R.id.tvRmbSymbol);
        }
    }

}
